package www.yingdi365.com.activity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.amap.api.maps2d.model.LatLng;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpHeaders;

import java.util.Calendar;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;
import www.yingdi365.com.R;
import www.yingdi365.com.base.ZjbBaseActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.model.AppInvitationSave;
import www.yingdi365.com.model.AppInvitationSaveResult;
import www.yingdi365.com.model.AppInvitationTmpl;
import www.yingdi365.com.model.AppInvitationTmplPaging;
import www.yingdi365.com.model.AppMusic;
import www.yingdi365.com.model.OkObject;
import www.yingdi365.com.util.GsonUtils;
import www.yingdi365.com.util.IsMobileNOorPassword;
import www.yingdi365.com.util.LogUtil;

public class SheZhiQJActivity extends ZjbBaseActivity implements View.OnClickListener {

    private TextView textViewRight;
    private EditText editBoy;
    private EditText editBoyMobile;
    private EditText editGirl;
    private EditText editGirlMobile;
    private EditText editHotel;
    private EditText editTreatAddress;
    private TextView textTreatDate;
    private TextView textTreatAddressLocation;
    private AppInvitationTmplPaging.TmplsBean tmplsBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_she_zhi_qj);
        init();
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        tmplsBean = (AppInvitationTmplPaging.TmplsBean) intent.getSerializableExtra(Constant.INTENT_KEY.TmplsBean);
    }

    @Override
    protected void findID() {
        textViewRight = (TextView) findViewById(R.id.textViewRight);
        editBoy = (EditText) findViewById(R.id.editBoy);
        editBoyMobile = (EditText) findViewById(R.id.editBoyMobile);
        editGirl = (EditText) findViewById(R.id.editGirl);
        editGirlMobile = (EditText) findViewById(R.id.editGirlMobile);
        editHotel = (EditText) findViewById(R.id.editHotel);
        editTreatAddress = (EditText) findViewById(R.id.editTreatAddress);
        textTreatDate = (TextView) findViewById(R.id.textTreatDate);
        textTreatAddressLocation = (TextView) findViewById(R.id.textTreatAddressLocation);
    }

    @Override
    protected void initViews() {
        ((TextView) findViewById(R.id.textViewTitle)).setText("设置请柬");
        textViewRight.setText("保存");
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageBack).setOnClickListener(this);
        findViewById(R.id.textViewRight).setOnClickListener(this);
        findViewById(R.id.viewDiTu).setOnClickListener(this);
        textTreatDate.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.REQUEST_RESULT_CODE.POSITION && resultCode == Constant.REQUEST_RESULT_CODE.POSITION) {
            LatLng position = data.getParcelableExtra(Constant.INTENT_KEY.Position);
            textTreatAddressLocation.setText(position.latitude + "," + position.longitude);
        }
    }

    private OkObject getOkObject() {
        String url = Constant.HOST + Constant.Url.AppInvitationService + "invitation/save";
        HttpHeaders headers = new HttpHeaders();
        headers.put(Constant.TOKEN, appLoginResult.getToken());
        HashMap<String, String> params = new HashMap<>();
        return new OkObject(headers, params, url);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.viewDiTu:
                Intent intent = new Intent();
                intent.setClass(this, DiTuActivity.class);
                startActivityForResult(intent, Constant.REQUEST_RESULT_CODE.POSITION);
                break;
            case R.id.textTreatDate:
                Calendar c = Calendar.getInstance();
                new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, final int year, final int month, final int dayOfMonth) {
                        new TimePickerDialog(SheZhiQJActivity.this, new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                textTreatDate.setText(year + "-" + (month + 1) + "-" + dayOfMonth+" "+hourOfDay+":"+minute);
                            }
                        },12,0,true).show();
                    }
                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
                break;
            case R.id.textViewRight:
                if (TextUtils.isEmpty(editBoy.getText().toString().trim())) {
                    Toast.makeText(SheZhiQJActivity.this, "请填写新郎名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!IsMobileNOorPassword.isMobileNO(editBoyMobile.getText().toString().trim())) {
                    Toast.makeText(SheZhiQJActivity.this, "请正确填写新郎手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(editGirl.getText().toString().trim())) {
                    Toast.makeText(SheZhiQJActivity.this, "请填写新娘", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!IsMobileNOorPassword.isMobileNO(editGirlMobile.getText().toString().trim())) {
                    Toast.makeText(SheZhiQJActivity.this, "请正确填写新娘手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(textTreatDate.getText().toString().trim())) {
                    Toast.makeText(SheZhiQJActivity.this, "请选择婚礼日期", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(editHotel.getText().toString().trim())) {
                    Toast.makeText(SheZhiQJActivity.this, "请填写酒店名称", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(editTreatAddress.getText().toString().trim())) {
                    Toast.makeText(SheZhiQJActivity.this, "请填写婚礼地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(textTreatAddressLocation.getText().toString().trim())) {
                    Toast.makeText(SheZhiQJActivity.this, "请选择婚礼坐标", Toast.LENGTH_SHORT).show();
                    return;
                }
                AppMusic music = tmplsBean.getMusic();
                AppInvitationTmpl appInvitationTmpl = new AppInvitationTmpl(tmplsBean.getHtmlUrl(), tmplsBean.getId(), music, tmplsBean.getName(), tmplsBean.getPath(), tmplsBean.getPathContent(), tmplsBean.getThumb(), tmplsBean.getTmplData(), tmplsBean.getTmplDataEntity(), tmplsBean.getType());
                AppInvitationSave appInvitationSave = new AppInvitationSave(
                        editBoy.getText().toString().trim(),
                        editBoyMobile.getText().toString().trim(),
                        editGirl.getText().toString().trim(),
                        editGirlMobile.getText().toString().trim(),
                        editHotel.getText().toString().trim(),
                        "",
                        music,
                        tmplsBean.getName(),
                        tmplsBean.getTmplData(),
                        appInvitationTmpl,
                        editTreatAddress.getText().toString().trim(),
                        textTreatAddressLocation.getText().toString().trim(),
                        textTreatDate.getText().toString().trim(),
                        tmplsBean.getType());
                LogUtil.LogShitou("SheZhiQJActivity--onClick请柬保存对象", GsonUtils.parseObject(appInvitationSave));
                showLoadingDialog();
                OkObject okObject = getOkObject();
                OkGo.post(okObject.getUrl())//
                        .tag(this)//
                        .headers(okObject.getHeaders())
                        .upJson(GsonUtils.parseObject(appInvitationSave))//
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                cancelLoadingDialog();
                                LogUtil.LogShitou("SheZhiQJActivity--onSuccess", "请柬保存" + s);
                                try {
                                    AppInvitationSaveResult appInvitationSaveResult = GsonUtils.parseJSON(s, AppInvitationSaveResult.class);
                                    if (appInvitationSaveResult.isSuccess()) {
                                        Intent intent = new Intent();
                                        intent.setClass(SheZhiQJActivity.this, BianJiQJYLActivity.class);
                                        intent.putExtra(Constant.INTENT_KEY.ID, appInvitationSaveResult.getContent().getId());
                                        startActivity(intent);
                                        finish();
                                    } else {
                                        Toast.makeText(SheZhiQJActivity.this, appInvitationSaveResult.getMessage(), Toast.LENGTH_SHORT).show();
                                    }
                                } catch (Exception e) {
                                    Toast.makeText(SheZhiQJActivity.this, getResources().getString(R.string.shujuchucuo), Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                                cancelLoadingDialog();
                                if (response != null) {
                                    if (response.code() == 401) {
                                        showReLoginDialog();
                                    }
                                }
                                Toast.makeText(SheZhiQJActivity.this, getResources().getString(R.string.qingqiushibai), Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
            case R.id.imageBack:
                finish();
                break;
        }
    }
}
